define(
		[ "PDUtilDir/dataService", "PDAppDir/properties", "PDUtilDir/util",
				"PDModuleDir/fullcalendar/moment.min",
				"PDModuleDir/fullcalendar/fullcalendar.min",
				"PDModuleDir/fullcalendar/demo-to-codepen",
				"css!PDModuleDir/fullcalendar/fullcalendar.min",
				"css!PDModuleDir/fullcalendar/demo-topbar",
//				 "Date",
//				"DateCN", "css!DateCss",
				"css!AppPathDir/product/searchProductForSelfInfos/views/css/css" ],
		function( DataService, Properties, Util, Moment) {
			function getQueryString(name) {
				var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
				var r = window.location.search.substr(1).match(reg);
				if (r != null)
					return unescape(r[2]);
				return null;
			}
			HomeApp.controller('BodyCtrl',["$scope", "$compile", '$sce', function($scope, $compile, $sce) {
				$scope.entity = {visitor : []};
				$scope.cache = {
					productId : getQueryString("id")
				};
				$.ajax({
					url : getServer() + "/flea/hclv/product/queryDetailById.htmls",
					method : 'post',
					data : {
						'id' : $scope.cache.productId
					},
					success : function(data) {
                        if (data.status != 200) {
                            return;
                        }
						$scope.entity = data.data;
						$scope.entity.resources[0].path = Properties.URL_FILE_SERVER_URL + $scope.entity.resources[0].path;
						$scope.entity.product.feature = $sce.trustAsHtml($scope.entity.product.feature);
						$scope.entity.product.guarantee = $sce.trustAsHtml($scope.entity.product.guarantee);
						$scope.renderDataFun();
						$scope.entity.costs_str = $scope.entity.costs;
						$scope.entity.notes_str = $scope.entity.notes;
						$scope.entity.costs = $sce.trustAsHtml($scope.entity.costs);
						$scope.entity.notes = $sce.trustAsHtml($scope.entity.notes);
						$scope.$digest();
					}
				});
				$.ajax({
					url : getServer() + "/flea/hclv/getTourism.htmls",
					success : function(data) {
                        if (data.status != 200) {
                            return;
                        }
//						$scope.cache.types = data.data;
						var types = data.data, len = types.length, obj = new Map();
						for(var i = 0; i < len; i++){
							obj.set(types[i].dictCode, types[i].dictName);
						}
						$scope.cache.typeObject = obj;
						$scope.$digest();
					}
				});
//				$scope.reserveSupport = {
//					run : function() {
//						JSON.stringify($scope.entity.product);
//						DataService.setObject("product.search.reserve", {
//							timeTable: $scope.cache.timeTable,
//							entity: $scope.entity
//						});
//						window.parent.location.href = getServer() + '/flea/home.htmls#/reserve';
//						window.top.tabs.closeTab($scope.cache.productId);
//					}
//				};
				$scope.reserveSupport = {
					run : function() {
						$scope.entity.product.typeName = $scope.cache.typeObject.get($scope.entity.product.type);
						DataService.setObject("product.search.reserve", {
							timeTable: $scope.cache.timeTable,
							entity: $scope.entity
						});
						window.top.tabs.addTab({
							id : $scope.entity.product.id + '_reserve',
							title : '预定',
							remote : {
								url : getServer() + "/static/core/page/home/subHome.jsp",
								iframe : true,
								param : {
									html : getServer() + "/static/app/product/reserveInfos/views/index.html",
									ctrl : getServer() + "/static/app/product/reserveInfos/reserveSupport.js",
									id: $scope.entity.product.id
								}
							}
						});
						window.top.tabs.closeTab($scope.entity.product.id);
					}
				};
				$scope.renderDataFun = function(){
					var tmp_data = $scope.entity.timetables, len = tmp_data.length, events = new Array(), price = undefined, tmp_date = undefined, tmp_ticket = undefined;
					for(var i = 0; i < len; i++){
						tmp_date = tmp_data[i].date.split(" ")[0];
						price = tmp_data[i].salePriceAdult;
						tmp_ticket = tmp_data[i].restPersonNum;
						if (tmp_ticket > 50){
							tmp_ticket = '充足';
						}
						events.push({
							title: '充足',
							start: tmp_date
						},{
							title: '￥' + price,
							start: tmp_date
						});
					}
					$('#calendar').fullCalendar({
				        selectable: true,
				        header: {
				          left: 'prev,next today',
				          right: 'prev,next'
				        },
				        dayClick: function(date) {
				            var date_str = date.format(), data = $scope.entity.timetables, len = data.length;
						    for(var i = 0; i < len; i++){
							    if(data[i].date == date_str + " 00:00:00"){
								    $scope.cache.timeTable = data[i];
								    $scope.$digest();
								    return;
							    }
						    }
						    Util.alert('无此班期，请选择其它时间！');
				        },
				        eventLimit: false,
				        events: events
				    });
					tmp_data = $scope.entity.explains, len = tmp_data.length;
					for(var i = 0; i < len; i++){
						$scope.entity[tmp_data[i].uuid] = tmp_data[i].description;
					}
				}
			}
	]);
});